package main

import (
	"context"
	"git.900sui.cn/kc/base/utils"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/comtreeData"
	"git.900sui.cn/kc/rpcinterface/client/order"
	"git.900sui.cn/kc/rpcinterface/client/risk"
	"github.com/streadway/amqp"
	"strconv"
)
//消费成功,统计消费次数，门店耗卡金额
func main() {
	rpcOrder := new(order.DayStatistics).Init()

	rpcbbaa := new(risk.BusBasicAccumulative).Init()
	defer rpcbbaa.Close()

	rpcComtreeData := new(comtreeData.Consumption).Init()
	defer rpcComtreeData.Close()

	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	new(utils.KcLog).GetInstance(kcgin.AppConfig.String("order.log.consumeSucNumAndCardAmountCount"))
	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("order.queue.consumeSucNumAndCardAmountCount"), func(msg amqp.Delivery) {
		consumeLogId, _ := strconv.Atoi(string(msg.Body))

		reply := false
		err := rpcOrder.StatisConsume(context.Background(), &consumeLogId, &reply)
		if err != nil {
			utils.Logerr.Printf("消费成功，商家门店(消费次数，耗卡金额)数据统计任务执行失败，consumeLogId:%d, err:%s", consumeLogId, err)
		}
		logs.Info("消费成功，商家门店(消费次数，耗卡金额)数据统计任务执行成功，consumeLogId:%d",consumeLogId)

		// 添加 预付卡风险管理系统---统计累计已兑付（消费）金额  更新累计兑付率
		// 所辖门店平均年限
		if err = rpcbbaa.CashCardAssets(context.Background(),&consumeLogId,&reply);err != nil {
			logs.Error("已兑付（消费）金额到风险管理系统，失败信息：\n", consumeLogId, err.Error())
		}
		//添加 预付卡消费
		if err = rpcComtreeData.AddConsumptionRpc(context.Background(),&consumeLogId,&reply); err != nil {
			logs.Error("添加 预付卡消费，失败信息：\n", consumeLogId, err.Error())
		}
	})

	if err != nil {
		utils.Logerr.Printf("消费成功后的商家门店(消费次数，耗卡金额)数据统计任务执行错误, err: %s", err.Error())
	}
}
